import csv, simplejson

def import_json(json_file, csv_file):
    flds = 'key    roadway    description    end_date    projectno    adaparking    reviewstatus    geometry    record_status    project    garage    y    x    sidewalk    start_date    pm'.split()
    f = open(json_file)
    content = '\n'.join(f.readlines())
    print content
    json = simplejson.loads(content.replace('json=', ''))
    f.close()
    
    f = open(csv_file, 'w')
    writer = csv.writer(f, lineterminator='\n')
    writer.writerow(flds)
    for record in json['features']:
        fe = record['properties']
        row = []
        for fld in flds:
            if fld=='key':
                v = None
            elif fld=='end_date':
                v = fe['end']
            elif fld=='start_date':
                v = fe['start']
            elif fld=='projectno':
                v = fe['projectid']
            elif fld=='reviewstatus':
                v = 'Public'
            elif fld=='geometry':
                v = simplejson.dumps(record['geojson'])
            elif fld=='record_status':
                v = 'existing'
            elif fld=='project':
                v = fe['summary']
            elif fld=='pm':
                v = fe['PM']
            elif fld in ['roadway', 'garage', 'adaparking', 'sidewalk']:
                if fe[fld]=='YES':
                    v = 'TRUE'
                else:
                    v = 'FALSE'
            else:
                v = fe[fld]
            row.append(v)
        writer.writerow(row)
    f.close()
        
        


json_file = 'json.js'
csv_file = 'meltest.csv'
import_json(json_file, csv_file)